/*
 * @Author: your name
 * @Date: 2020-05-14 21:41:34
 * @LastEditTime: 2020-05-14 22:26:57
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ts\ts_test\generics.ts
 */
  function echo<T>(arg:T):T{
    console.log(arg)
    return arg
  }
  echo<number>(123)
  echo<string>("123")

  function swap<T,U>(tuple:[T,U]) : [U,T]{
    return [tuple[1],tuple[0]]
  }
  const User :[string,number] = swap<number,string>([10,'yihu'])
  console.log(User)

  function echoWithArr<T>(arg:T[]):number  {
    return arg.length
  }
  console.log(echoWithArr<string>(["111",'222']))

  interface Args {
    length:number
  }
  //约束泛型
  function echoWithLengh<T extends Args>(arg:T) : number {
    return arg.length
  }
  const arrLength:number = echoWithLengh<number[]>([1,2,3])
  const stringLength:number = echoWithLengh<string>("123")

  //泛型 类 接口
  class Queue<T> {
    private data:T[] = [];
    push(item:T){
      this.data.push(item)
    }
    pop():T{
      return this.data.shift()
    }
  }
  const queue = new Queue<number>()
  queue.push(3333.335)
  queue.push(44444)
  console.log(queue.pop().toFixed(2)) //3333.34

  const queue2 = new Queue<string>()
  queue2.push("3333.335")
  queue2.push("44444")
  console.log(queue2.pop().length) //8

  interface KeyPair<T,U> {
    key: T,
    value : U
  }
  const myArr:KeyPair<number,number>[] = [{key:1,value:1},{key:2,value:2}]
  const myArr2:KeyPair<number,string>[] = [{key:1,value:"1"},{key:2,value:"2"}]


  interface Iplus<T> {
    (a:T):T
  }

  const a:Iplus<number> = (a:number):number =>{
    return a
  }
  console.log(a(5))
